﻿///-------------------------------------------------------------------------
//高级搜索 表格Table排序JS  张生  【2010年11月27日 23:26:48 整理修改】第一版
//--------------------------------------------------------------------------
/*描述：
----------------------------------------------------------------------------*/

//点击列头排序
/*使用说明  :
方法1:
new TableSorter("tb1");
效果:
id为tb1的table的第一行任意单元格都可以点击进行排序.
方法2:
new TableSorter("tb1", 0, 1, 3);
效果:
id为tb1的table的第一行0,1,3单元格可以进行点击排序.
*/
//开始计算排序时间
var calcTime = new CalcExecuteTime();

function TableSorter(table) {
    this.Table = this.$(table);
    this.TableHeader = this.$("mainTableHeader");
    if (this.Table==null||this.Table.rows.length < 1) {
        return;
    }
    this.Init(arguments);
}
//以下样式针对表头的单元格.
TableSorter.prototype.NormalCss = "NormalCss"; //没有执行排序时的样式.
TableSorter.prototype.SortAscCss = "SortAscCss"; //升序排序时的样式.
TableSorter.prototype.SortDescCss = "SortDescCss"; //降序排序时的样式.
//初始化table的信息和操作.
TableSorter.prototype.Init = function (args) {
    this.ViewState = [];
    for (var x = 1; x < this.TableHeader.rows[0].cells.length; x++) {
        this.ViewState[x] = false;
    }

    for (var x = 1; x < this.TableHeader.rows[0].cells.length; x++) {
        this.TableHeader.rows[0].cells[x].onclick = this.GetFunction(this, "Sort", x);
        this.TableHeader.rows[0].cells[x].style.cursor = "pointer";
    }

}
//简写document.getElementById方法.
TableSorter.prototype.$ = function (element) {
    return document.getElementById(element);
}
//取得指定对象的脱壳函数.
TableSorter.prototype.GetFunction = function (variable, method, param, param1, param2) {
    return function () {
        variable[method](param, param1, param2);
    }
}
//执行排序.
TableSorter.prototype.Sort = function (col) {
    add_loading();
    var SortAsNumber = true;
  
    for (var x = 0; x < this.TableHeader.rows[0].cells.length - 1; x++) {
        this.TableHeader.rows[0].cells[x].className = this.NormalCss;
    }
    var Sorter = [];
    for (var x = 0; x < this.Table.rows.length - 1; x++) {
        var val = this.Table.rows[x].cells[col].innerHTML.replace(/<.+?>/gim, "").replace("&nbsp;", "").Trim();
        if (val == "--") {
            val = -10000;
        }
        Sorter[x] = [val, x];
        SortAsNumber = SortAsNumber && this.IsNumeric(Sorter[x][0]);
    }
    if (SortAsNumber) {
        for (var x = 0; x < Sorter.length; x++) {
            for (var y = x + 1; y < Sorter.length; y++) {
                if (parseFloat(Sorter[y][0]) < parseFloat(Sorter[x][0])) {
                    var tmp = Sorter[x];
                    Sorter[x] = Sorter[y];
                    Sorter[y] = tmp;
                }
            }
        }
    }
    else {
        Sorter.sort(function (a, b) { return a[0].localeCompare(b[0]) });
    }

    for (var x = 0; x < this.ViewState.length; x++) {
        if (x == col) continue;
        this.ViewState[x] = false;
        if (x > 2) this.ViewState[x] = true;
    }

    if (this.ViewState[col]) {
        Sorter.reverse();
        this.ViewState[col] = false;
    }
    else {
        this.ViewState[col] = true;
    }
    var Rank = [];
    var titRank = [];
    for (var x = 0; x < Sorter.length; x++) {
        Rank[x] = this.GetRowHtml(this.Table.rows[Sorter[x][1]]);
        titRank[x] = this.Table.rows[Sorter[x][1]].title;
    }
    setTimeout(this.GetFunction(this, "afterSort", Rank, col, titRank), 100);

}
TableSorter.prototype.afterSort = function (Rank, col, titRank) {
 
    for (var x = 0; x < this.Table.rows.length-1; x++) {
        for (var y = 0; y < this.Table.rows[x].cells.length; y++) {
            this.Table.rows[x].cells[y].innerHTML = Rank[x][y];
        }
        this.Table.rows[x].title = titRank[x];
        this.Table.rows[x].cells[0].innerHTML = x+1;
    }
    this.OnSorted(this.TableHeader.rows[0].cells[col], this.ViewState[col]);
    remove_loading();
}

//取得指定行的内容.
TableSorter.prototype.GetRowHtml = function (row) {
    var result = [];
    for (var x = 0; x < row.cells.length; x++) {
        result[x] = row.cells[x].innerHTML;
    }
    return result;
}
TableSorter.prototype.IsNumeric = function (num) {
    //    return /^[+-]\d+(\.\d+)?$/.test(num);
    return /^(\+|-)?\d+(\.\d+)?(%|次)?$/.test(num);

}
//可自行实现排序后的动作.
TableSorter.prototype.OnSorted = function (cell, IsAsc) {
    return;
}


//隐藏loading...层
function remove_loading() {
    var targelem = document.getElementById("loader_gys_container");
    if (targelem != null) {
        targelem.style.display = "none";
        targelem.style.visibility = "hidden";
    }
    calcTime.Print("divPageLoadTime");
}
//加载loading...层
function add_loading() {
    calcTime.Begin();
    var targelem = document.getElementById("loader_gys_container");
    if (targelem != null) {
        targelem.style.display = "block";
        targelem.style.visibility = "visible";
    }
}